
// ---------------------------
// 扩展模块：UI特效管理器

import { instantiate, Prefab, Node, Vec3 } from "cc";
import { ResLoader } from "../ResLoader";

// ---------------------------
export class UIEffectManager {
    private static effects: Map<string, Prefab> = new Map();

    // 预加载常用特效
    public static async preloadCommonEffects(): Promise<void> {
        const effects = ['effect_click', 'effect_transition', 'effect_levelup'];
        for (const effect of effects) {
            await this.loadEffect(effect);
        }
    }

    // 加载特效
    public static async loadEffect(effectName: string): Promise<Node | null> {
        if (this.effects.has(effectName)) {
            const prefab = this.effects.get(effectName);
            return prefab ? instantiate(prefab) : null;
        }

        const prefab = await ResLoader.loadPrefab(effectName, 'effects')
        if (prefab) {
            this.effects.set(effectName, prefab);
            let node = instantiate(prefab);
            return node;
        }

        return null;
    }
    // 在UI上播放特效
    public static async playEffectOnUI(effectName: string, uiNode: Node, position?: Vec3): Promise<Node | null> {
        const effect = await this.loadEffect(effectName);
        if (effect) {
            uiNode.addChild(effect);
            if (position) {
                effect.setPosition(position);
            }
            return effect;
        }
        return null;
    }
}
